package com.example.webpractice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.webpractice.po.TijiaozuoyePO;
import com.example.webpractice.service.TijiaozuoyeService;
import com.example.webpractice.utils.ServerResponseEntity;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 提交作业
 * 后端接口
 */
@RestController
@RequestMapping("/tijiaozuoye")
public class TijiaozuoyeController {
    @Autowired
    private TijiaozuoyeService tijiaozuoyeService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public ServerResponseEntity page(@RequestParam(name = "page") Integer page,
                                     @RequestParam(name = "limit") Integer limit,
                                     @RequestParam(name = "kechengmingcheng", required = false) String kechengmingcheng,
                                     @RequestParam(name = "zuoyemingcheng", required = false) String zuoyemingcheng,
                                     @RequestParam(name = "jiaoshixingming", required = false) String jiaoshixingming,
                                     @RequestParam(name = "xueshengxingming", required = false) String xueshengxingming,
                                     HttpServletRequest request) {
        QueryWrapper<TijiaozuoyePO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(kechengmingcheng)) {
            queryWrapper.like("kechengmingcheng", kechengmingcheng);
        }
        if (StringUtils.isNotBlank(zuoyemingcheng)) {
            queryWrapper.like("zuoyemingcheng", zuoyemingcheng);
        }
        if (StringUtils.isNotBlank(jiaoshixingming)) {
            queryWrapper.like("jiaoshixingming", jiaoshixingming);
        }
        if (StringUtils.isNotBlank(xueshengxingming)) {
            queryWrapper.like("xueshengxingming", xueshengxingming);
        }
        String tableName = (String) request.getAttribute("tableName");
        String username;
        if (tableName.equals("jiaoshi")) {
            username = (String) request.getAttribute("username");
            queryWrapper.eq("gonghao", username);
        }
        tableName = request.getAttribute("tableName").toString();
        if (tableName.equals("xuesheng")) {
            queryWrapper.eq("xuehao", request.getAttribute("username"));
        }
        return ServerResponseEntity.success(tijiaozuoyeService.queryPage(page, limit, queryWrapper));
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Long[] ids) {
        if (tijiaozuoyeService.deleteBatchIds(Arrays.asList(ids)) != ids.length) {
            return ServerResponseEntity.fail(-1, "删除失败");
        }
        return ServerResponseEntity.success();
    }
}
